package com.work.rcj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.work.rcj.common.base.BaseController;
import com.work.rcj.common.model.Response;
import com.work.rcj.common.model.page.PageResult;
import com.work.rcj.entity.Medical;
import com.work.rcj.entity.Notice;
import com.work.rcj.pojo.dto.MedicalDto;
import com.work.rcj.pojo.dto.NoticeDto;
import com.work.rcj.service.MedicalService;
import com.work.rcj.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 *  通知通告管理
 *
 * @author rcj
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/notice")
public class NoticeController extends BaseController {
    @Autowired
    private NoticeService noticeService;


    /**
     * 通知通告列表
     *
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public Response<PageResult<Notice>> list(NoticeDto dto) {
        Page<Notice> page = new Page<>(dto.getPageIndex(), dto.getPageSize());
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("deleted",1);
        if(!StringUtils.isEmpty(dto.getSearchKey())){
            queryWrapper.like("name",dto.getSearchKey());
        }
        IPage<Notice> list = noticeService.page(page, queryWrapper);
        return Response.success(getDataTable(list));
    }

    /**
     * 新增通知通告
     *
     * @param req
     * @return
     */
    @PostMapping("/save")
    public Response save(@RequestBody Notice req) {
        noticeService.save(req);
        return Response.success();
    }

    /**
     * 修改通知通告信息
     *
     * @param req
     * @return
     */
    @PostMapping("/update")
    public Response update(@RequestBody Notice req) {
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", req.getId());
        noticeService.update(req, updateWrapper);
        return Response.success();
    }

    /**
     * 删除通知通告
     *
     * @param req
     * @return
     */
    @PostMapping("/del")
    public Response del(@RequestBody Notice req) {
        Notice entity = new Notice();
        entity.setId(req.getId());
        entity.setDeleted("1");
        noticeService.updateById(entity);
        return Response.success();
    }

    /**
     * 通知通告详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Response<Notice> detail(Long id) {
        Notice medical = noticeService.getById(id);
        return Response.success(medical);
    }
}

